package com.twu.biblioteca;

import com.twu.biblioteca.libraryitems.Book;
import com.twu.biblioteca.libraryitems.LibraryItem;
import com.twu.biblioteca.libraryitems.Movie;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private static Library library;
    private static UserManager userManager;

    public static void main(String[] args) {

        setUpLibrary();

        if (login()){
            startLibrary();
        }
    }

    private static void setUpLibrary() {
        List<Customer> libraryCustomers = new ArrayList<Customer>();
        libraryCustomers.add(new Customer("Sarah", "123-0001", "12345", "caixeta.sarah@gmail.com", "8561546"));

        List<LibraryItem> libraryItems = new ArrayList<LibraryItem>();
        libraryItems.add(new Book("Head First - Java", "Kathy Sierra"));
        libraryItems.add(new Movie("The Hobbit", "2012", "Peter Jackson", 9));

        library = new Library(libraryCustomers, libraryItems);

        userManager = new UserManager(library);
    }

    private static boolean login() {
        System.out.println("Please, inform you library credentials.");
        System.out.println("Library number: ");
        String libraryNumber = readString();

        System.out.println("Password: ");
        String password = readString();

        return userManager.login(libraryNumber, password);
    }

    private static void startLibrary() {
        System.out.println("The library is ready! Choose an option.");

        while(true){
            showMenuOptions();
            manageAction(readOption());
        }
    }

    private static void showMenuOptions(){
        StringBuilder menuOptions = new StringBuilder();
        menuOptions.append("1 - List the available books.\n");
        menuOptions.append("2 - List the available movies.\n");
        menuOptions.append("3 - Checkout a item.\n");
        menuOptions.append("4 - Return a item.\n");
        menuOptions.append("5 - See Personal Information.\n");

        System.out.println(menuOptions.toString());

    }

    private static void manageAction(int option) {
        if (option == 1)
            showLibraryItems(Book.class);
        else if (option == 2)
            showLibraryItems(Movie.class);
        else if (option == 3)
            checkoutItem();
        else if (option == 4)
            returnItem();
        else if (option == 5)
            showUserInformation();
        else
            System.out.println("Select a valid option!");

    }

    private static void showLibraryItems(Class<? extends LibraryItem> itemClass){
        String listTitle = "Available ".concat(itemClass.getSimpleName()).concat(" List");
        System.out.println(listTitle);
        for (LibraryItem book : library.getAvailableItemsByType(itemClass)){
            System.out.println(book.toString());
        }
        System.out.println("-*-*-");
    }

    private static void checkoutItem(){
        System.out.println("Please enter the code of the item.");
        int itemID = readOption();

        LibraryItem requiredItem = library.getItem(itemID);
        if (library.checkoutItem(userManager.getCurrentUser(), requiredItem))
            System.out.println("Thank you! Enjoy...");
        else
            System.out.println("That item is not available.");
    }

    private static void returnItem(){
        System.out.println("Please enter the code of the item you want to return.");
        int itemID = readOption();

        LibraryItem requiredItem = library.getItem(itemID);
        if (library.returnItem(userManager.getCurrentUser(), requiredItem))
            System.out.println("Thank you for returning the item.");
        else
            System.out.println("That is not a valid item to return.");
    }

    private static void showUserInformation() {
        System.out.println(userManager.getUserInfo());
    }

    private static String readString(){
        Scanner keyboardScanner = new Scanner(System.in);
        return keyboardScanner.nextLine();
    }

    private static int readOption(){
        Scanner keyboardScanner = new Scanner(System.in);
        return keyboardScanner.nextInt();
    }

}
